package com.kye.express.business.order.order_address;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kye.express.R;
import com.kye.express.databinding.ItemCompanyBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.order.OrderAddresses;

import me.drakeet.multitype.ItemViewBinder;

public class CompanyViewBinder extends ItemViewBinder<OrderAddresses, CompanyViewBinder.ViewHolder> {

    private OnItemClickListener<OrderAddresses> mOnItemClickListener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_company, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OrderAddresses orderAddresses) {
        holder.bind(orderAddresses);
    }

    @Override
    protected long getItemId(@NonNull OrderAddresses item) {
        return item.getCompanyCode().hashCode();
    }

    public OnItemClickListener<OrderAddresses> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<OrderAddresses> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemCompanyBinding mBinding;
        private OrderAddresses mData;

        ViewHolder(View itemView) {
            super(itemView);
            mBinding = ItemCompanyBinding.bind(itemView);
            mBinding.imageButtonAddAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener == null) return;

                    mOnItemClickListener.onClickItem(mData, view);
                }
            });
        }

        public void bind(OrderAddresses data) {
            mData = data;
            mBinding.setCompany(data.getCompanyName());
        }
    }
}
